package com.slt.linux.controller;

import com.slt.linux.dto.ExcelVo;
import com.slt.linux.util.ExcelFactory;
import com.slt.linux.util.EasyExcelUtil;
import com.slt.linux.util.HuToolExcelUtil;
import com.slt.linux.util.OssUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Random;
import static com.slt.linux.util.EasyExcelUtil.*;

@RestController
public class ExportExcelController {
    /**
     *
     * 原版
     * @return
     * @throws IOException
     */
    @GetMapping(value = "testExportExcel")
    public String testExportExcel() throws IOException {
        ExcelFactory<ExcelVo> excelFactory = new ExcelFactory();
        //从数据库查询数据
        List<ExcelVo> excelVoList = getExcelVoList();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        String fileName = "数据分析表_测试" + new Random().nextInt(10000) + System.currentTimeMillis() + ".xlsx";
        //生成excel
        excelFactory.createExportExcel().createExcel(out, excelVoList, ExcelVo.class);
        //上传到oss
        OssUtil ossUtil = new OssUtil();
        String url = ossUtil.uploadExcel2OSS(new ByteArrayInputStream(out.toByteArray()), fileName);
        //该url可直接返回给前端下载
        String returnUrl = url.substring(0, url.lastIndexOf("?")) + ".xlsx";
        return returnUrl;
    }

    /**
     * 使用oss服务器返回的url 进行下载
     * @return
     * @throws IOException
     */
    @GetMapping(value = "testExportExcel2")
    public String testExportExcel2() throws IOException {
        ExcelFactory<ExcelVo> excelFactory = new ExcelFactory();
        //从数据库查询数据
        List<ExcelVo> excelVoList = getExcelVoList();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        String fileName = "数据分析表_测试" + new Random().nextInt(10000) + System.currentTimeMillis() + ".xlsx";
        //生成excel
        EasyExcelUtil<ExcelVo> exportEsUtil = new EasyExcelUtil<>();
        exportEsUtil.createExcel(excelVoList, ExcelVo.class, fileName, out);

        //上传到oss
        String url = "";

        //该url可直接返回给前端下载
        String returnUrl = url.substring(0, url.lastIndexOf("?")) + ".xlsx";
        return returnUrl;
    }

    /**
     * 重定向response 完成下载
     * @param httpServletResponse
     * @return
     * @throws IOException
     */
    @GetMapping(value = "testExportExcel3")
    public String testExportExcel3(HttpServletResponse httpServletResponse) throws IOException {
        ExcelFactory<ExcelVo> excelFactory = new ExcelFactory();
        //从数据库查询数据
        List<ExcelVo> excelVoList = getExcelVoList();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        String fileName = "数据分析表_测试" + new Random().nextInt(10000) + System.currentTimeMillis() + ".xlsx";
        //生成excel
        EasyExcelUtil<ExcelVo> exportEsUtil = new EasyExcelUtil<>();
        exportEsUtil.exportExcel(excelVoList, ExcelVo.class, fileName, httpServletResponse);

        //上传到oss
//        OssUtil ossUtil = new OssUtil();
//        String url = ossUtil.uploadExcel2OSS(new ByteArrayInputStream(out.toByteArray()), fileName);
        System.out.println("hello");

        return "test export excel";
    }

    @GetMapping(value = "testExportExcel4")
    public String testExportExcel4(HttpServletResponse response) throws IOException {
        HuToolExcelUtil<ExcelVo> huToolExcelUtil = new HuToolExcelUtil<>();
        //从数据库查询数据
        List<ExcelVo> excelVoList = getExcelVoList();

        String fileName = "数据分析表_测试" + new Random().nextInt(10000) + System.currentTimeMillis() + ".xlsx";
        FileOutputStream fileOutputStream = new FileOutputStream(fileName);
        //生成excel
        huToolExcelUtil.createExcel(fileOutputStream,excelVoList,ExcelVo.class);


        //上传到oss
        System.out.println("hello");

        return "test export excel";
    }

}
